package com.zhangsun.read;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.*;

public class ReadWeb {

    private File folder;

    private BufferedWriter out;

    public static ReadWeb newInstance(File folder, BufferedWriter out) {
        ReadWeb readWeb = new ReadWeb();
        readWeb.folder = folder;
        readWeb.out = out;
        return readWeb;
    }

    public void readWeb() throws IOException {
        Comparator<File> comparator = Comparator.comparingLong(File::lastModified);
        List<File> files = Arrays.asList(Objects.requireNonNull(folder.listFiles()));
        files.sort(comparator);
        for (File file : files) {
            if (file.isFile()) {
                Document document = Jsoup.parse(file, "utf-8");
                Elements titles = document.select("div#content > h2");
                if (titles != null && titles.size() > 0) {
                    StringBuilder titleContent = new StringBuilder();
                    for (int i = 0; i < titles.size(); i++) {
                        String content = getHtmlContent(titles.get(i).childNodes());
                        titleContent.append(content);
                        if (i == titles.size() - 1) {
                            titleContent.append("\r\n");
                        } else {
                            titleContent.append(" ");
                        }
                    }
                    out.write(titleContent.toString());
                    out.write("\r\n");
                }
                Elements divs = document.select("div#content > div");
                if (divs != null && divs.size() > 0) {
                    for (int i = 0; i < divs.size(); i++) {
                        if (i % 6 == 0) {
                            String content = getHtmlContent(divs.get(i).childNodes());
                            out.write("\t" + content + "\r\n");
                            out.write("\r\n");
                        }
                    }
                }

                out.write("\r\n");
            }
        }
    }

    private final List<String> tags = Arrays.asList("strong", "span", "small");

    private String getHtmlContent(List<Node> nodes) {
        StringBuilder content = new StringBuilder();
        if (nodes != null && nodes.size() > 0) {
            for (Node node : nodes) {
                if (node instanceof TextNode) {
                    content.append(((TextNode) node).text());
                } else if (node instanceof Element) {
                    if (((Element) node).hasClass("notetext")) {
                        content.append("（注：").append(node.attr("data-note")).append("）");
                    } else if (tags.contains(((Element) node).tagName())) {
                        content.append(((Element) node).text());
                    }
                }
            }
        }
        return content.toString();
    }

}
